import {getWxQrCodeList, createWxQrCode, getSwitchStatus, getKeywordReply, setQrKeyword, getQrKeyword, getwxFansTagList,
        addQrTags, delQrTags, getQrReply, setQrReply, getSourceMoreInfo, delQrKeyword} from '@/service/getData'
import * as type from '@/store/mutation-types'
const state = {
  qrList: {
    list: [],
    page: {}
  },
  keywordList: {},
  tagsList: [],
  subscribeReply: {}
}
const actions = {
  // 获取参数列表
  async getQrList ({commit, state}, payload) {
    let res = await getWxQrCodeList(payload)
    commit(type.GET_QRLIST, res.data.data)
  },
  async newQrCode ({commit, state}, payload) {
    let res = await createWxQrCode(payload.appid, payload.wd)
    return res
  },
  async switchActiveStatus ({commit, state}, payload) {
    let res = await getSwitchStatus(payload.qrId, payload.status)
    return res
  },
  async getKwList ({commit, state}, appid) {
    let res = await getKeywordReply({ appid: appid })
    commit(type.GET_QRKWLIST, res.data.data)
    return res
  },
  async setQrKeyword ({commit, state}, payload) {
    let res = await setQrKeyword(payload.qrId, payload.keywordId)
    return res
  },
  async delQrKeyword ({commit, state}, qrId) {
    let res = await delQrKeyword(qrId)
    return res
  },
  async getQrKeyword ({commit, state}, payload) {
    let res = await getQrKeyword(payload)
    return res
  },
  async getTagList ({commit, state}, appid) {
    let res = await getwxFansTagList(appid)
    commit(type.GET_QRTAGLIST, res.data.data)
  },
  async addQrTags ({commit, state}, payload) {
    let res = await addQrTags(payload)
    return res
  },
  async delQrTags ({commit, state}, payload) {
    let res = await delQrTags(payload)
    return res
  },
  async getQrReply ({commit, state}, qrId) {
    let res = await getQrReply(qrId)
    if (res.data.data && res.data.data.msgType !== 'text') {
      let res2 = await getSourceMoreInfo(res.data.data.mediaid)
      Object.assign(res.data.data, res2.data.data)
      commit(type.SET_QR_REPLY, {qrId: qrId, data: res.data.data})
    } else if (res.data.data) {
      commit(type.SET_QR_REPLY, {qrId: qrId, data: res.data.data})
    }
    return res
  },
  async setQrReply ({commit, state}, payload) {
    let res = await setQrReply(payload)
    return res
  }
}
const mutations = {
  [type.GET_QRLIST] (state, payload) {
    /* payload.list.forEach((item) => {
      if (!item.keyword) {
        item.keyword = ''
      }
    }) */
    state.qrList = payload
  },
  [type.GET_QRKWLIST] (state, payload) {
    state.keywordList = payload
  },
  [type.GET_QRTAGLIST] (state, payload) {
    state.tagsList = payload.tags
  },
  [type.SET_QR_REPLY] (state, payload) {
    state.qrList.list.map(item => {
      if (item.id === payload.qrId) {
        item.reply = {...payload.data}
      }
    })
    state.qrList.list = [...state.qrList.list]
  }
}
export default {
  state,
  actions,
  mutations
}
